package com.itc.canvas;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

import com.itc.display.Main;
import com.itc.resource.LoadResource;
import com.itc.resource.SoundManager;
import com.itc.score.HighScore;

public class GameUICanvas extends GameCanvas implements CommandListener {
	private Main midlet;
	private int width;
	private Graphics g;
	private int height;
	private int k;
	private int R, G, B;
	private Random rand;
	private Command backCmd;
	private int score = -1;
	private int Num1, Num2, Num3;
	private boolean _result, first = true;
	HighScore highScore = new HighScore();
	private SoundManager sound;
	private Player player;
	protected GameUICanvas(Main midlet) {
		super(true);
		this.midlet = midlet;
		rand = new Random();
		initViews();
		
		sound = new SoundManager();
		player = sound.getPlayer("/sfx_point.mp3");
		newQuestion();
	}

	public void initViews() {
		width = getWidth();
		height = getHeight();
		backCmd = new Command("Back", Command.BACK, 1);
		addCommand(backCmd);
		setCommandListener(this);
	}

	private void newQuestion() {
		score++;
		highScore.setScore(score);
		k = 40;
		R = LoadResource.GRB[rand.nextInt(4)];
		G = LoadResource.GRB[rand.nextInt(4)];
		B = LoadResource.GRB[rand.nextInt(4)];
		do {
			Num1 = rand.nextInt(10);
		} while (Num1 == 0);
		do {
			Num2 = rand.nextInt(10);
		} while (Num2 == 0);
		if (rand.nextInt(100) > 60) {
			_result = true;
			Num3 = Num1 + Num2;
		} else {
			_result = false;
			if ((Num1 > 5) && (Num2 > 5))
				do {
					Num3 = rand.nextInt(15);
				} while ((Num3 < 10) || (Num3 == Num1 + Num2) || (Num3 < Num1)
						|| (Num3 < Num2));
			else
				do {
					Num3 = rand.nextInt(10);
				} while ((Num3 < 5) || (Num3 == Num1 + Num2) || (Num3 < Num1)
						|| (Num3 < Num2));
		}
	}

	public void paint(Graphics g) {
		g.setColor(R, G, B);
		g.fillRect(0, 0, width, height);
		answer(g);
		drawQuestion(g);
		drawScoreCurrent(g);
		if (!first)
			progessbar(g);
	}

	public void drawScoreCurrent(Graphics g) {
		Font font = g.getFont();
		int mSizeText = font.stringWidth("Score: " + score);
		g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC
				| Font.STYLE_BOLD, Font.SIZE_LARGE));
		g.setColor(0xffffff);
		g.drawString("Score: " + score, 10, 10, Graphics.LEFT | Graphics.TOP);
	}

	private void drawQuestion(Graphics g) {
		g.drawImage(LoadResource.mImgNumArr[Num1], 70, 60, Graphics.LEFT
				| Graphics.TOP);
		g.drawImage(LoadResource.mImgNumArr[10], 105, 60, Graphics.LEFT
				| Graphics.TOP);
		g.drawImage(LoadResource.mImgNumArr[Num2], 140, 60, Graphics.LEFT
				| Graphics.TOP);
		g.drawImage(LoadResource.mImgNumArr[11], 85, 110, Graphics.LEFT
				| Graphics.TOP);
		if (Num3 < 10)
			g.drawImage(LoadResource.mImgNumArr[Num3], 120, 110, Graphics.LEFT
					| Graphics.TOP);
		else {
			g.drawImage(LoadResource.mImgNumArr[Num3 / 10], 120, 110,
					Graphics.LEFT | Graphics.TOP);
			g.drawImage(LoadResource.mImgNumArr[Num3 % 10], 155, 110,
					Graphics.LEFT | Graphics.TOP);
		}
	}

	public void progessbar(Graphics g) {
		if (k >= 0)
			k--;
		if (k < 0) {
			// System.out.println("Thuaaaa");
			resultUI();
		}
		try {
			Thread.sleep(25);
			g.setColor(0xffffff);
			fillProgressBar(g, 0, 0, 240, 10, k, 40);
			repaint();
		} catch (Exception ex) {
		}
	}

	public void resultUI() {

		Display display = Display.getDisplay(this.midlet);
		ResultUICanvas resultUICanvas = new ResultUICanvas(this.midlet);
		display.setCurrent(resultUICanvas);
	}

	public void answer(Graphics g) {
		g.drawImage(LoadResource.mImgTrue, 20, height - 60 - 10, Graphics.TOP
				| Graphics.LEFT);
		g.drawImage(LoadResource.mImgFalse, width - 60 - 20, height - 60 - 10,
				Graphics.TOP | Graphics.LEFT);
	}

	void fillProgressBar(Graphics g, int x, int y, int w, int h, int p, int t) {
		g.drawRect(x, y, w, h);
		p = (p * w) / t;
		g.fillRect(x, y, p, h);
	}

	protected void pointerPressed(int x, int y) {
		// Pressed true
		System.out.println(x + "," + y);
		if (x >= 20 && y >= height - 60 - 10 && x <= 20 + 60
				&& y <= height - 60 - 10 + 60) {
			first = false;
			check_Ans(true);
		}
		// Pressed false
		if (x >= width - 60 - 20 && y >= height - 60 - 10
				&& x <= width - 60 - 20 + 60 && y <= height - 60 - 10 + 60) {
			first = false;
			check_Ans(false);
		}
	}

	private void check_Ans(boolean _user) {
		if (_user == _result) {
//			try {
//				desSound();
//			} catch (Exception e) {
//			}
			try {
				player.start();
			} catch (MediaException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			newQuestion();
			repaint();
		} else {
			// System.out.println("Thuaa11");
			resultUI();
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c == backCmd) {
			Display display = Display.getDisplay(this.midlet);
			MenuCanvas menuCanvas = new MenuCanvas(this.midlet);
			display.setCurrent(menuCanvas);
		}

	}
}
